package com.hck.taozi_help.module.main;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hck.taozi_help.module.main.jsfunction.MainWebViewJs;
import com.hck.taozi_help.module.webview.BaseWebViewActivity;
import com.hck.taozi_help.module.webview.BaseX5WebViewActivity;
import com.hck.taozi_help.module.yuyin.KeDaYuYingManger;
import com.hck.taozi_help.module.yuyin.KeDaYuYingPlayVoice;
import com.hck.taozi_help.utils.LogUtil;
import com.hck.taozi_help.utils.permission.PermissionUtil;
import com.hck.taozi_help.utils.permission.interfaces.IPermissionCallback;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsListener;

import java.util.Set;

public class MainActivity extends BaseX5WebViewActivity {
    private String url="https://peach-genie-test.100cbc.com/genie" ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LogUtil.log("onCreateonCreateonCreate: ");
        super.onCreate(savedInstanceState);
        init();
        boolean canLoadX5 = QbSdk.canLoadX5(getApplicationContext());
        PermissionUtil.applyAudioPermission(this, new IPermissionCallback() {
            @Override
            public void onPermissionResult(boolean result, Set<String> dataSet) {
                LogUtil.log("onPermissionResult: "+result);
            }
        });
//        QbSdk.setTbsListener(new TbsListener() {
//            @Override
//            public void onDownloadFinish(int i) {
//                // 下载结束时的状态，下载成功时errorCode为100,其他均为失败，外部不需要关注具体的失败原因
//               LogUtil.log("onDownloadFinish -->下载X5内核完成：" + i);
//
//            }
//
//            @Override
//            public void onInstallFinish(int i) {
//                // 安装结束时的状态，安装成功时errorCode为200,其他均为失败，外部不需要关注具体的失败原因
//                LogUtil.log("onInstallFinish -->安装X5内核进度：" + i);
//                if (i == 200) {
//                    Toast.makeText(getApplicationContext(), " 安装X5内核进度 " + i, Toast.LENGTH_SHORT).show();
//                    //TODO  todo somthing here
//                }
//            }
//
//            @Override
//            public void onDownloadProgress(int i) {
//                // 下载过程的通知，提供当前下载进度[0-100]
//                LogUtil.log("onDownloadProgress " + i);
//            }
//        });
//        if (!canLoadX5) {
//            return;
//        }
        if (keDaYuYingManger==null){
            keDaYuYingManger=new KeDaYuYingManger();
        }
        loadUrl(url);

    }

    @Override
    protected void onStop() {
        super.onStop();
        initWeakUp();
    }

    private void init(){
        mViewView.addJavascriptInterface(new MainWebViewJs(this), MainWebViewJs.APP);
    }
     private KeDaYuYingManger keDaYuYingManger;
    private void initWeakUp(){

        keDaYuYingManger.start(this, new KeDaYuYingManger.OnCallBackListener() {
            @Override
            public void wakeApp() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.log("wakeAppwakeAppwakeApp");
                        Intent intent = new Intent(MainActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        keDaYuYingManger.stop();
                    }
                });

            }
        });
    }



    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (keDaYuYingManger!=null){
            keDaYuYingManger.stop();
        }
        LogUtil.log("onNewIntentonNewIntent");
    }


}
